package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author Lachlan McInnes
	 */
	public class ExtrasArtworkState extends FlxState
	{
		private var _backButton:FlxButtonPlus;
		private var _backText1:FlxText;
		private var _backText2:FlxText;
		
		private var _warehouseBackground:FlxSprite;
		private var _warehouseBackCrates:FlxSprite;
		private var _warehouseFrontCrates:FlxSprite;
		
		protected var _cloudLow1:FlxSprite;
		protected var _cloudMid1:FlxSprite;
		protected var _cloudHigh1:FlxSprite;
		protected var _citySkyline1:FlxSprite;
		protected var _citySkyline2:FlxSprite;
		protected var _citySkyline3:FlxSprite;
		protected var _cityLights:FlxSprite;
		
		protected var _cloudLow2:FlxSprite;
		protected var _cloudMid2:FlxSprite;
		protected var _cloudHigh2:FlxSprite;
		protected var _fairSkyline1:FlxSprite;
		protected var _fairLights:FlxSprite;
		
		public var logoSprite:FlxSprite;
		
		private var _buttonPressed:Boolean = false;
		private var _fadeSprite:FlxSprite;
		private var _fadeTimer:Number = 0.0;
		private const _fadeTimeMax:Number = 1.0;
		
		public function ExtrasArtworkState(artNumber:uint) 
		{
			switch(artNumber)
			{
			case 0:
				loadWarehouse();
				break;
			case 1:
				loadCity();
				break;
			case 2:
				loadFair();
				break;
			case 3:
				loadLogo();
				break;
			}
			
			_backButton = new FlxButtonPlus(2, FlxG.height - 25, goToExtras);
			_backText1 = new FlxText(0, 0, 100, "Back").setFormat("PowellAntique", 15, 0xDDDDDD, "center", 0xFF000000);
			_backText2 = new FlxText(_backText1.x, _backText1.y, _backText1.width, _backText1.text).setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_backButton.loadGraphic(new FlxSprite().loadGraphic(ImageFiles.button), new FlxSprite().loadGraphic(ImageFiles.buttonHighlight));
			_backButton.setMouseOverCallback(mouseOverSound);
			_backButton.loadText(_backText1, _backText2);
			add(_backButton);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 1;
			add(_fadeSprite);
		}
		
		public function loadWarehouse():void
		{
			_warehouseBackground = new FlxSprite(0, 0, ImageFiles.warehouseBackground);
			add(_warehouseBackground);
			
			var window:FlxSprite = new FlxSprite(FlxG.width / 4 - 78, 90, ImageFiles.warehouseWindow);
			add(window);
			window = new FlxSprite(FlxG.width / 2 - 39, 90, ImageFiles.warehouseWindow);
			add(window);
			window = new FlxSprite(3 * FlxG.width / 4, 90, ImageFiles.warehouseWindow);
			add(window);
			
			_warehouseBackCrates = new FlxSprite(0, FlxG.height, ImageFiles.warehouseBackCrates);
			_warehouseBackCrates.y -= _warehouseBackCrates.height;
			add(_warehouseBackCrates);
			
			_warehouseFrontCrates = new FlxSprite(0, FlxG.height, ImageFiles.warehouseFrontCrates);
			_warehouseFrontCrates.y -= _warehouseFrontCrates.height;
			add(_warehouseFrontCrates);
		}
		
		public function loadCity():void
		{
			_cloudHigh1 = new FlxSprite(0, FlxG.height, ImageFiles.cityCloudHigh);
			_cloudHigh1.y -= _cloudHigh1.height;
			_cloudMid1 = new FlxSprite(0, FlxG.height, ImageFiles.cityCloudMid);
			_cloudMid1.y -= _cloudMid1.height;
			_cloudLow1 = new FlxSprite(0, FlxG.height, ImageFiles.cityCloudLow);
			_cloudLow1.y -= _cloudLow1.height;
			var cityBackground:FlxSprite = new FlxSprite(0, FlxG.height, ImageFiles.cityBackground);
			cityBackground.y -= cityBackground.height;
			_cityLights = new FlxSprite(0, 0, ImageFiles.cityLights);
			_cityLights.scrollFactor.x = _cityLights.scrollFactor.y = 0
			_citySkyline1 = new FlxSprite(0, FlxG.height, ImageFiles.citySkyline);
			_citySkyline1.y -= _citySkyline1.height;
			
			add(_cloudHigh1);
			add(_cloudMid1);
			add(_cloudLow1);
			add(cityBackground);
			add(_cityLights);
			add(_citySkyline1);
		}
		
		public function loadFair():void
		{
			_cloudHigh2 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudHigh);
			_cloudHigh2.y -= _cloudHigh2.height;
			_cloudMid2 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudMid);
			_cloudMid2.y -= _cloudMid2.height;
			_cloudLow2 = new FlxSprite(0, FlxG.height, ImageFiles.fairCloudLow);
			_cloudLow2.y -= _cloudLow2.height;
			var fairBackground:FlxSprite = new FlxSprite(0, FlxG.height, ImageFiles.fairBackground);
			fairBackground.y -= fairBackground.height;
			_fairLights = new FlxSprite(0, FlxG.height, ImageFiles.fairLights);
			_fairLights.y -= _fairLights.height;
			_fairSkyline1 = new FlxSprite(0, FlxG.height, ImageFiles.fairSkyline);
			_fairSkyline1.y -= _fairSkyline1.height;
			
			add(_cloudHigh2);
			add(_cloudMid2);
			add(_cloudLow2);
			add(fairBackground);
			add(_fairLights);
			add(_fairSkyline1);
		}
		
		private function loadLogo():void
		{
			logoSprite = new FlxSprite(0, 0, ImageFiles.omnistrainLogoImage);
			add(logoSprite);
		}
		
		override public function update():void 
		{
			super.update();
			
			if (_buttonPressed)
			{
				fadeOut();
			}
			else
			{
				fadeIn();
			}
		}
		
		private function goToExtras():void
		{
			if (!_buttonPressed)
			{
				FlxG.play(SoundFiles.selectSound, .75);
				_buttonPressed = true;
			}
		}
		
		private function fadeOut():void
		{
			_fadeTimer = _fadeSprite.alpha / _fadeTimeMax;
			_fadeTimer += FlxG.elapsed;
			
			if (_fadeTimer / _fadeTimeMax > _fadeSprite.alpha)
			{
				_fadeSprite.alpha = _fadeTimer / _fadeTimeMax;
			}
			
			if (_fadeTimer >= _fadeTimeMax)
			{
				FlxG.state = new ExtrasState(true);
			}
		}
		
		private function fadeIn():void
		{
			if (_fadeTimer < _fadeTimeMax && !_buttonPressed)
			{
				_fadeTimer += FlxG.elapsed;
				
				_fadeSprite.alpha = Math.abs(1 - (_fadeTimer / _fadeTimeMax));
			}
		}
		
		private function mouseOverSound():void
		{
			FlxG.play(SoundFiles.mouseOverSound, 0.75);
		}
		
	}

}